package org.agile4j.xsd;

import org.agile4j.xsd.baseenumtype.ParseBaseEnumTypeUtil;
import org.agile4j.xsd.basetype.ParseBaseTypeUtil;
import org.agile4j.xsd.dict.ParseDictUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;


/**
 * 文件解析工具
 *
 * @author hanyx
 */
public final class ParserUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(ParserUtil.class);

    public static Map<String, org.agile4j.xsd.dict.Schema> deplogyDict(File... files) {
        Map<String, org.agile4j.xsd.dict.Schema> map = new HashMap<>();
        if (files != null && files.length > 0) {
            try {
                for (File innerFile : files) {
                    if (innerFile.exists() && innerFile.isFile()) {// 文件对象存
                        String xmlFilePath = innerFile.getAbsolutePath();
                        org.agile4j.xsd.dict.Schema sql = ParseDictUtil.getDictConfig(xmlFilePath);
                        map.put(xmlFilePath, sql);
                    } else if (innerFile.exists() && innerFile.isDirectory()) {
                        Collection<File> filesCollection = FileUtils.listFiles(innerFile, new String[]{"d_schema.xml"}, true);
                        if (CollectionUtils.isNotEmpty(filesCollection)) {
                            for (File file : filesCollection) {
                                String xmlFilePath = file.getAbsolutePath();
                                org.agile4j.xsd.dict.Schema sql = ParseDictUtil.getDictConfig(xmlFilePath);
                                map.put(xmlFilePath, sql);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                LOGGER.error("deplogy Table failur", e);
                throw new RuntimeException(e);
            }
        }
        return map;
    }

    public static Map<String, org.agile4j.xsd.basetype.Schema> deplogyBaseType(File... files) {
        Map<String, org.agile4j.xsd.basetype.Schema> map = new HashMap<>();
        if (files != null && files.length > 0) {
            try {
                for (File innerFile : files) {
                    if (innerFile.exists() && innerFile.isFile()) {// 文件对象存
                        String xmlFilePath = innerFile.getAbsolutePath();
                        org.agile4j.xsd.basetype.Schema sql = ParseBaseTypeUtil.getBaseTypeConfig(xmlFilePath);
                        map.put(xmlFilePath, sql);
                    } else if (innerFile.exists() && innerFile.isDirectory()) {
                        Collection<File> filesCollection = FileUtils.listFiles(innerFile, new String[]{"u_schema.xml"}, true);
                        if (CollectionUtils.isNotEmpty(filesCollection)) {
                            for (File file : filesCollection) {
                                String xmlFilePath = file.getAbsolutePath();
                                org.agile4j.xsd.basetype.Schema sql = ParseBaseTypeUtil.getBaseTypeConfig(xmlFilePath);
                                map.put(xmlFilePath, sql);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                LOGGER.error("deplogy deplogyBaseType failur", e);
                throw new RuntimeException(e);
            }
        }
        return map;
    }

    public static Map<String, org.agile4j.xsd.baseenumtype.Schema> deplogyBaseEnumBaseType(File... files) {
        Map<String, org.agile4j.xsd.baseenumtype.Schema> map = new HashMap<>();
        if (files != null && files.length > 0) {
            try {
                for (File innerFile : files) {
                    if (innerFile.exists() && innerFile.isFile()) {// 文件对象存
                        String xmlFilePath = innerFile.getAbsolutePath();
                        org.agile4j.xsd.baseenumtype.Schema sql = ParseBaseEnumTypeUtil.getBaseEnumTypeConfig(xmlFilePath);
                        map.put(xmlFilePath, sql);
                    } else if (innerFile.exists() && innerFile.isDirectory()) {
                        Collection<File> filesCollection = FileUtils.listFiles(innerFile, new String[]{"e_schema.xml"}, true);
                        if (CollectionUtils.isNotEmpty(filesCollection)) {
                            for (File file : filesCollection) {
                                String xmlFilePath = file.getAbsolutePath();
                                org.agile4j.xsd.baseenumtype.Schema sql = ParseBaseEnumTypeUtil.getBaseEnumTypeConfig(xmlFilePath);
                                map.put(xmlFilePath, sql);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                LOGGER.error("deplogy baseEnumBaseType failur", e);
                throw new RuntimeException(e);
            }
        }
        return map;
    }

}
